package datastructure.tree;

/**
 * @author MaoLin Wang
 * @date 2020/10/7 11:59 下午
 */
public class BinaryTree {
    TreeNode root;

    public BinaryTree(TreeNode root) {
        this.root = root;
    }

    public void preOrder(){
        if (root!=null){
            this.preOrder(this.root);
        }
    }

    private void preOrder(TreeNode root) {
        if (root==null){
            return;
        }
        System.out.println(root.val);
        preOrder(root.lchild);
        preOrder(root.rchild);
    }

    public static void main(String[] args) {

        //创建根节点
        TreeNode root = new TreeNode(11);
        TreeNode treeNode2 = new TreeNode(21);
        TreeNode treeNode3 = new TreeNode(31);
        TreeNode treeNode4 = new TreeNode(14);
        TreeNode treeNode5 = new TreeNode(15);
        TreeNode treeNode6 = new TreeNode(61);
        TreeNode treeNode7 = new TreeNode(71);
        TreeNode treeNode8 = new TreeNode(81);
        TreeNode treeNode9 = new TreeNode(91);

        root.lchild =treeNode2;
        root.rchild =treeNode3;
        treeNode3.rchild =treeNode7;
        treeNode3.lchild =treeNode6;
        treeNode2.lchild =treeNode4;
        treeNode2.rchild =treeNode5;
        treeNode4.lchild =treeNode8;
        treeNode4.rchild =treeNode9;
        BinaryTree binaryTree=new BinaryTree(root);


        System.out.println("中序-------");
        binaryTree.midOrder();

        System.out.println("前序--------");
        binaryTree.preOrder();

        System.out.println("后续--------");
        binaryTree.postOrder();

    }

    private void postOrder() {
        if (root==null){
            return;
        }
        this.postOrder(root);
    }

    private void postOrder(TreeNode root) {
        if (root==null){
            return;
        }
        postOrder(root.lchild);
        postOrder(root.rchild);
        System.out.println(root.val);
    }

    public void midOrder() {
        if (root==null){
            return;
        }
        this.midOrder(this.root);
    }

    private void midOrder(TreeNode root) {
        if (root==null){
            return;
        }
        midOrder(root.lchild);
        System.out.println(root.val);
        midOrder(root.rchild);
    }
}
class TreeNode{
   public int val;
    TreeNode lchild;
    TreeNode rchild;

    public TreeNode(int val) {
        this.val = val;
    }
}
